<!DOCTYPE HTML>
<html>

<head>
    <meta charset="utf-8">
    <meta name="renderer" content="webkit|ie-comp|ie-stand">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
        content="width=device-width,initial-scale=1,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no" />
    <meta http-equiv="Cache-Control" content="no-siteapp" />
    <link rel="stylesheet" type="text/css" href="../static/h-ui/css/H-ui.min.css" />
    <link rel="stylesheet" type="text/css" href="../static/h-ui.admin/css/H-ui.admin.css" />
    <link rel="stylesheet" type="text/css" href="../lib/Hui-iconfont/1.0.8/iconfont.css" />
    <link rel="stylesheet" type="text/css" href="../static/h-ui.admin/skin/default/skin.css" id="skin" />
    <link rel="stylesheet" type="text/css" href="../static/h-ui.admin/css/style.css" />
    <title>用户日报告</title>
    <style>
        .pagecontent {
            width: 100%;
            height: 100%;
            padding: 20px;
            box-sizing: border-box;
        }

        .pagecontent .toolarea {
            display: block;
            line-height: 40px;
        }

        .toolarea .spanstate {
            font-size: 14px;
            cursor: pointer;
            padding-left: 10px;
            padding-right: 10px;
        }

        .toolarea input[type=button] {
            display: inline-block;
            padding: 4px;
            border: 0;
            background-color: rgb(63, 218, 89);
            border-radius: 2px;
            cursor: pointer;
            padding-left: 20px;
            padding-right: 20px;
            color: white;
        }

        .toolarea .selected {
            font-weight: bold;
        }

        #chartarea {
            width: 100%;
            height: 500px;
            position: relative;
            margin: auto;
        }

        .selectUser {
            line-height: 30px;
            width: 200px;
            text-align: center;
            height: 30px;
        }
    </style>

</head>

<body>


    <div class="pagecontent">

        <div class="searcharea">
            <div class="st">
                <div class="sn">等级报表</div>
                <div class="idesc">日志等级的报表情况</div>
            </div>
            <form id="formsearch">
                <div class="searchbox">

                    <select name="appname">
                        <option value="SmartShopApi">SmartShopApi</option>
                    </select>

                    <div class="timearea" style="display:flex;border:1px solid #888;border-radius: 4px;">
                        <select name="timetype" style="width:120px;border:none;outline:none;text-align: center;">
                            <option value="empty">报表时间</option>
                        </select>
                        <input type="text" name="daterange" readonly datas="" datae="" value=""
                            style="position:relative;flex:1;outline:none;border:none;border-radius: 4px; text-align: center;"
                            id="picker_date_range">
                    </div>
                    <input type="text" class="inputword" title="关键字搜索" placeholder="关键字搜索" value="">
                    <input type="button" class="btnsearch" onclick="_funcreadtoday(1);" value="查询">
                </div>
            </form>
            <a class="btn btn-success radius r" style="line-height:1.6em;margin-top:3px"
                href="javascript:location.replace(location.href);" title="刷新"><i class="Hui-iconfont">&#xe68f;</i></a>
        </div>

        <!-- 工具选择区域 -->
        <div id="chartarea">

        </div>

        <div id="mainchart" style="width:100%;position:relative;height: 500px;"></div>

    </div>


    <!--_footer 作为公共模版分离出去-->
    <!-- <script type="text/javascript" src="../lib/jquery/1.9.1/jquery.min.js"></script> -->
    <script type="text/javascript" src="../lib/pickerdaterange/jquery.min.js"></script>
    <script type="text/javascript" src="../lib/layer/2.4/layer.js"></script>
    <script type="text/javascript" src="../static/h-ui/js/H-ui.min.js"></script>
    <!-- <script type="text/javascript" src="../lib/My97DatePicker/4.8/WdatePicker.js"></script> -->
    <script type="text/javascript" src="../lib/jquery.contextmenu/jquery.contextmenu.r2.js"></script>
    <script type="text/javascript" src="../static/h-ui.admin/js/H-ui.admin.js"></script>

    <!--/_footer 作为公共模版分离出去-->
    <!--请在下方写此页面业务相关的脚本-->
    <!-- <script type="text/javascript" src="../lib/My97DatePicker/4.8/WdatePicker.js"></script> -->
    <script type="text/javascript" src="../lib/api.js"></script>
    <!-- <link href="../lib/page.min.css" rel="stylesheet" />
    <script src="../lib/page.min.js"></script> -->
    <script type="text/javascript" src="echarts.min.js"></script>
    <script type="text/javascript" src="../lib/pickerdaterange/dateRange.js"></script>
    <link rel="stylesheet" href="../lib/pickerdaterange/dateRange.css">

    <script>

        // var pageobject = null;

        var issecond = true;
        var containername = "dockertest";

        var linuxid = 0;

        function _showpagedata(datas) {

            var option = {
                title: {
                    text: ''
                },
                tooltip: {
                    trigger: 'axis',
                    formatter: function (params) {
                        var relVal = params[0].name;
                        for (var i = 0, l = params.length; i < l; i++) {
                            if (params[i].seriesName.indexOf("耗时") >= 0) {
                                relVal += '<br/>' + params[i].marker + params[i].seriesName + "：" + params[i].value + "小时"
                            } else {
                                relVal += '<br/>' + params[i].marker + params[i].seriesName + "：" + params[i].value
                            }
                            // if (i == 1 || i == 2) {
                            //     relVal += '<br/>' + params[i].marker + params[i].seriesName +"："+ params[i].value + "小时"
                            // } else {
                            //     relVal += '<br/>' + params[i].marker + params[i].seriesName +"："+ params[i].value
                            // }
                        }
                        return relVal;
                    }
                },
                toolbox: {
                    show: true,
                    feature: {
                        magicType: { show: true, type: ['stack', 'tiled'] },
                        saveAsImage: { show: true }
                    }
                },
                legend: {
                    data: ['工单件数', '耗时', '平均耗时', '回复数', '总工单数']
                },
                xAxis: {
                    data: []
                },
                yAxis: [
                    {
                        type: 'value',
                        name: '工单件数',
                        position: 'left',
                        offset: 0,
                        axisLabel: {
                            formatter: '{value}单'
                        }
                    },
                    {
                        type: 'value',
                        name: '耗时',
                        position: 'left',
                        offset: 60,
                        axisLabel: {
                            formatter: '{value}小时'
                        }
                    },
                    {
                        type: 'value',
                        name: '平均耗时',
                        position: 'right',
                        nameLocation: 'end',
                        offset: 0,
                        axisLabel: {
                            formatter: '{value}小时'
                        }
                    },
                    {
                        type: 'value',
                        name: '回复数',
                        position: 'right',
                        nameLocation: 'end',
                        offset: 60,
                        axisLabel: {
                            formatter: '{value}'
                        }
                    },
                    {
                        type: 'value',
                        name: '总工单数',
                        position: 'right',
                        nameLocation: 'end',
                        offset: -60,
                        axisLabel: {
                            formatter: '{value}个'
                        }
                    }
                ],
                series: []
            };

            // data: ['新单', '退款单', '退款金额', '完成单']

            var array1 = new Array();
            var array2 = new Array();
            var array3 = new Array();
            var array4 = new Array();
            var array5 = new Array();

            //data: ['工单件数', '耗时', '平均耗时', '回复数','总工单数']
            for (var k = 0; k < datas.length; k++) {
                var item = datas[k];
                option.xAxis.data.push(item.dataDate.toString().substr(0, 10));

                array1.push(item.totalSheetNum);
                array2.push(Number(item.totalMinute / 60).toFixed(2));
                array3.push(Number(item.avageMinute / 60).toFixed(2));
                array4.push(item.totalReply);
                array5.push(item.totalMainSheet);
            }

            //data: ['新单', '退款单', '退款金额', '完成单']
            option.series.push({ smooth: true, name: '工单件数', type: 'line', data: array1, yAxisIndex: 0 });
            option.series.push({ smooth: true, name: '耗时', type: 'line', data: array2, yAxisIndex: 1 });
            option.series.push({ smooth: true, name: '平均耗时', type: 'line', data: array3, yAxisIndex: 2 });
            option.series.push({ smooth: true, name: '回复数', type: 'line', data: array4, yAxisIndex: 3 });
            option.series.push({ smooth: true, name: '总工单数', type: 'line', data: array5, yAxisIndex: 4 });

            // option.series.push({ smooth: true, name: '线程数', type: 'line', data: pidsnum, yAxisIndex: 4 });

            //pidsNum

            //初始化echarts实例
            var myChart = echarts.init(document.getElementById('chartarea'));

            //使用制定的配置项和数据显示图表
            myChart.setOption(option);



            // if(issecond){
            //     setTimeout(() => {
            //         _readpagedata(1);
            //     }, 30000);
            // }

        }
        $(window).ready(function () {
            var uidstr = _apigetquery("id");
            if (uidstr != null) {
                containername = (uidstr);
            }
            var linuxstr = _apigetquery("linuxid");
            if (linuxstr != null) {
                linuxid = parseInt(linuxstr);
            }
            var dateRange = new pickerDateRange('picker_date_range', {
                isTodayValid: false,
                defaultText: '  至  ',
                inputTrigger: 'input_trigger_demo3',
                success: function (obj) {
                    $("#picker_date_range").attr('datas', obj.startDate);
                    $("#picker_date_range").attr('datae', obj.endDate);
                },
                clear: function () {
                }
            });
            
            var daynow = new Date();
            daynow.setTime(daynow.getTime() - 3600000 * 24 * 31);
            var _s = daynow.Format("yyyy-MM-dd");
            var daylater = new Date();
            daylater.setTime(daylater.getTime() + 1000 * 3600);
            var _e = daylater.Format("yyyy-MM-dd");
            $("[name=daterange]").val(_s + " 至 " + _e);
            $("[name=daterange]").attr('datas', _s);
            $("[name=daterange]").attr('datae', _e);

            _apiget('/api/app/appInfo/options',false,(code,obj)=>{
                if(code==200){
                    if(obj){
                        if(obj.length>0){
                            var ahtml='';
                            obj.forEach(item=>{
                                ahtml+='<option value="'+item.appName+'">'+item.displayName+'</option>';
                            });
                            $("[name=appname]").html(ahtml);
                        }
                    }
                }
            });

            _funcreadtoday();

        });

        function _funcload() {
            issecond = false;
            $(".spanstate").removeClass("selected");
            _readpagedata(1);

        }
        function _funcreadtoday() {
            var _info = $("#formsearch").parseForm();
            if (_info.daterange) {
                if (_info.daterange.length > 0) {
                    _info.sdate = $("[name=daterange]").attr('datas') + " 00:00:00";
                    _info.edate = $("[name=daterange]").attr('datae') + " 00:00:00";
                }
            }
            $("table").find("tbody").empty();
            _apipost('/api/app/Report/Status', true, JSON.stringify(_info), (code, obj) => {
                if (code == 200) {
                    if (obj && obj.length > 0) {
                        buildecharts(obj);
                    }
                }
            });
        }
        function buildecharts(modellist) {
            var option = {
                title: {
                    text: ''
                },
                tooltip: {
                    trigger: 'axis'
                },
                toolbox: {
                    show: false,
                    feature: {
                        magicType: { show: false, type: ['stack', 'tiled'] },
                        saveAsImage: { show: false }
                    }
                },
                legend: {
                    data: []
                },
                xAxis: {
                    data: []
                },
                yAxis: [
                    {
                        type: 'value',
                        name: '数量',
                        position: 'left',
                        offset: 0,
                        axisLabel: {
                            formatter: '{value}'
                        }
                    }
                ],
                series: []
            };
            var zindex = 0;
            //时间数组
            var datearray = new Array();
            var datalist = new Array();
            //数据信息
            var datamap = new Map();
            modellist.forEach(item => {
                if (datearray.indexOf(item.dataDate) == -1) {
                    datearray.push(item.dataDate);
                }
                buildmap(datamap, 0, item.dataDate, item.statusCode + "", item.hitTime);
            });
            console.log(datamap);
            //写入数据项
            for (var ikey of datamap.keys()) {
                option.legend.data.push(ikey);
            }
            var dateline = datearray.sort();//X轴时间线
            //基于现有的x轴时间线，计算对应的数据 注意map的val是{infos:[],data:[]};
            for (var val of datamap.values()) {
                var tdata = new Array();
                dateline.forEach(vd => {
                    var vf = 0;
                    val.infos.forEach(vi => {
                        if (vi.date == vd) {
                            vf = vi.num;
                        }
                    });
                    tdata.push(vf);
                });
                val.data = tdata;
            }
            for (var key of datamap.keys()) {
                var info = datamap.get(key);
                option.series.push({ smooth: true, name: info.name, type: 'line', data: info.data, yAxisIndex: info.type });
            }
            option.xAxis.data = dateline;
            if (!option.legend.selected) {
                option.legend.selected = {};
            }
            //初始化echarts实例
            var myChart = echarts.init(document.getElementById('chartarea'));
            //使用制定的配置项和数据显示图表
            myChart.setOption(option);
        }
        function buildmap(datamap, type, date, key, num) {
            var vinfo = {
                infos: [{
                    date: date,
                    num: num,
                }],
                name: key,
                type: type,
                data: []
            };
            if (!datamap.has(key)) {
                datamap.set(key, vinfo);
            } else {
                var vlist = datamap.get(key).infos;
                //是否存在这个项 如果存在，则数量叠加
                var find = false;
                vlist.forEach(ii => {
                    if (ii.date == date) {
                        ii.num += num;
                        find = true;
                    }
                });
                if (!find) {
                    vlist.push({ date: date, num: num });
                }
            }
        }
    </script>
</body>
</html>